//
//  MCBodyDataStatistics Vc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/6/6.
//

import UIKit
import QMUIKit
import Toast_Swift
import SnapKit
import RxSwift
import RxCocoa
import SwiftyJSON
import ObjectMapper
import JXSegmentedView


class MCBodyDataStatisticsVc: MCBaseVc {


    private var statisticsDataSource:MCBodyDataStatisticsModelList?
    
    //体重
    @IBOutlet weak var weightStr: MCPaddingLabel!
    @IBOutlet weak var weightTrend: MCPaddingLabel!
    @IBOutlet weak var weightNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var weightBtn: UIButton!
    @IBOutlet weak var weightStatistView: UIView!
    @IBOutlet weak var weightLayout: NSLayoutConstraint!
    private var isDisPlayWeight:Bool = true
    private var weightLineChartView: MJLineChartView!
    
    
    
    //体脂
    @IBOutlet weak var fatLabel: MCPaddingLabel!
    @IBOutlet weak var fatreadLabel: MCPaddingLabel!
    @IBOutlet weak var fatNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var fatBtn: UIButton!
    @IBOutlet weak var fatStatistView: UIView!
    @IBOutlet weak var fatLayout: NSLayoutConstraint!
    private var isFat:Bool = true
    private var fatLineChartView:MJLineChartView!
    
    
    
    //身高
    
    @IBOutlet weak var heightLabel: MCPaddingLabel!
    @IBOutlet weak var heightreadLabel: MCPaddingLabel!
    @IBOutlet weak var heightNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var heightBtn: UIButton!
    @IBOutlet weak var heightStatistView: UIView!
    @IBOutlet weak var heightLayout: NSLayoutConstraint!
    private var isHeight:Bool = true
  //  private var heightLineChartView:MJLineChartView!
    
    
    //胸围
    @IBOutlet weak var bustLabel: MCPaddingLabel!
    @IBOutlet weak var bustreadLabel: MCPaddingLabel!
    @IBOutlet weak var bustNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var bustBtn: UIButton!
    @IBOutlet weak var bustStatistView: UIView!
    @IBOutlet weak var bustLayout: NSLayoutConstraint!
    private var isBust:Bool = true
    
    //腰围
    @IBOutlet weak var waistLabel: MCPaddingLabel!
    @IBOutlet weak var waistreadLabel: MCPaddingLabel!
    @IBOutlet weak var waistNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var waistBtn: UIButton!
    @IBOutlet weak var waistStatistView: UIView!
    @IBOutlet weak var waistLayout: NSLayoutConstraint!
    private var isWaist:Bool = true
    
    
    //腹围
    @IBOutlet weak var abdomenLabel: MCPaddingLabel!
    @IBOutlet weak var abdomenreadLabel: MCPaddingLabel!
    @IBOutlet weak var abdomenNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var abdomenBtn: UIButton!
    @IBOutlet weak var abdomenStatistView: UIView!
    @IBOutlet weak var abdomenLayout: NSLayoutConstraint!
    private var isAbdomen:Bool = true
    
    
    //臀围
    @IBOutlet weak var pygalLabel: MCPaddingLabel!
    @IBOutlet weak var pygalreadLabel: MCPaddingLabel!
    @IBOutlet weak var pygalNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var pygalBtn: UIButton!
    @IBOutlet weak var pygalStatistView: UIView!
    @IBOutlet weak var pygalLayout: NSLayoutConstraint!
    private var isPygal:Bool = true
    
    
    //手臂围
    @IBOutlet weak var armLabel: MCPaddingLabel!
    @IBOutlet weak var armreadLabel: MCPaddingLabel!
    @IBOutlet weak var armNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var armBtn: UIButton!
    @IBOutlet weak var armStatistView: UIView!
    @IBOutlet weak var armLayout: NSLayoutConstraint!
    private var isArm:Bool = true
    
    
    //大腿围
    @IBOutlet weak var thighLabel: MCPaddingLabel!
    @IBOutlet weak var thighreadLabel: MCPaddingLabel!
    @IBOutlet weak var thighNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var thighBtn: UIButton!
    @IBOutlet weak var thighStatistView: UIView!
    @IBOutlet weak var thighLayout: NSLayoutConstraint!
    private var isThigh:Bool = true
    
    
    //小腿围
    @IBOutlet weak var shankLabel: MCPaddingLabel!
    @IBOutlet weak var shankreadLabel: MCPaddingLabel!
    @IBOutlet weak var shankNoDataLabel: MCPaddingLabel!
    @IBOutlet weak var shankBtn: UIButton!
    @IBOutlet weak var shankStatistView: UIView!
    @IBOutlet weak var shankLayout: NSLayoutConstraint!
    private var isShank:Bool = true
    
     
    
    
    
    
    private let dispose:DisposeBag = DisposeBag()
    
    
    
    
  
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "数据统计"
        
        
        //体重
        self.weightBtn.rx.tap.subscribe(onNext: { event in
            if self.isDisPlayWeight == true {
                self.weightLayout.constant = 50
                self.weightStatistView.isHidden = true
                self.isDisPlayWeight = false
                self.weightBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.weightLayout.constant = 180
                self.weightStatistView.isHidden = false
                self.isDisPlayWeight = true
                self.weightBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        
//        weightLineChartView = MJLineChartView.init(frame: .zero)
//        var configuration = MJLineChartConfiguration()
//        configuration.yMin = 0
//        configuration.yMax = 50
//        configuration.ySegmentCount = 5
//        configuration.xMin = 1
//        configuration.xMax = 31
//        configuration.xSegmentCount = 1
//        configuration.yAxisAlarmLineMaxValue = 31
//        configuration.yAxisAlarmLineMinValue = 1
//        weightLineChartView.setupLineChartView(frame: weightStatistView.bounds, configuration: configuration)
//        weightStatistView.addSubview(weightLineChartView)
//
//        var pointArray: [MJLineChartLineSource] = []
//        var point: MJLineChartLineSource = MJLineChartLineSource()
//
//        point.xAxisValue = 1
//        point.yAxisValue = 20
//        point.xValue = "0"
//        pointArray.append(point)
//
//        weightLineChartView.addLine(values: pointArray)
//
        
        //体脂
        self.fatBtn.rx.tap.subscribe(onNext: { event in
            if self.isFat == true {
                self.fatLayout.constant = 50
                self.fatStatistView.isHidden = true
                self.isFat = false
                self.fatBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.fatLayout.constant = 180
                self.fatStatistView.isHidden = false
                self.isFat = true
                self.fatBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
//        fatLineChartView = MJLineChartView.init(frame: .zero)
//        var fatConfiguration = MJLineChartConfiguration()
//        fatConfiguration.yMin = 5
//        fatConfiguration.yMax = 50
//        fatConfiguration.ySegmentCount = 5
//        fatConfiguration.xMin = 1
//        fatConfiguration.xMax = 31
//        fatConfiguration.xSegmentCount = 1
//        fatConfiguration.yAxisAlarmLineMaxValue = 31
//        fatConfiguration.yAxisAlarmLineMinValue = 1
//        weightLineChartView.setupLineChartView(frame: fatStatistView.bounds, configuration: fatConfiguration)
//        fatStatistView.addSubview(fatLineChartView)
//
//        var fatPointArray: [MJLineChartLineSource] = []
//        var fatPoint: MJLineChartLineSource = MJLineChartLineSource()
//
//        fatPoint.xAxisValue = 1
//        fatPoint.yAxisValue = 20
//        fatPoint.xValue = "0"
//        fatPointArray.append(fatPoint)
//        fatLineChartView.addLine(values: fatPointArray)
        
        
        //身高
        self.heightBtn.rx.tap.subscribe(onNext: { event in
            if self.isHeight == true {
                self.heightLayout.constant = 50
                self.heightStatistView.isHidden = true
                self.isHeight = false
                self.heightBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.heightLayout.constant = 180
                self.heightStatistView.isHidden = false
                self.isHeight = true
                self.heightBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        //胸围
        self.bustBtn.rx.tap.subscribe(onNext: { event in
            if self.isBust == true {
                self.bustLayout.constant = 50
                self.bustStatistView.isHidden = true
                self.isBust = false
                self.bustBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.bustLayout.constant = 180
                self.bustStatistView.isHidden = false
                self.isBust = true
                self.bustBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        
        //腰围
        self.waistBtn.rx.tap.subscribe(onNext: { event in
            if self.isWaist == true {
                self.waistLayout.constant = 50
                self.waistStatistView.isHidden = true
                self.isWaist = false
                self.waistBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.waistLayout.constant = 180
                self.waistStatistView.isHidden = false
                self.isWaist = true
                self.waistBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        
        //腹围
        self.abdomenBtn.rx.tap.subscribe(onNext: { event in
            if self.isAbdomen == true {
                self.abdomenLayout.constant = 50
                self.abdomenStatistView.isHidden = true
                self.isAbdomen = false
                self.abdomenBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.abdomenLayout.constant = 180
                self.abdomenStatistView.isHidden = false
                self.isAbdomen = true
                self.abdomenBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        //臀围
        self.pygalBtn.rx.tap.subscribe(onNext: { event in
            if self.isPygal == true {
                self.pygalLayout.constant = 50
                self.pygalStatistView.isHidden = true
                self.isPygal = false
                self.pygalBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.pygalLayout.constant = 180
                self.pygalStatistView.isHidden = false
                self.isPygal = true
                self.pygalBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        
        //手臂围
        self.armBtn.rx.tap.subscribe(onNext: { event in
            if self.isArm == true {
                self.armLayout.constant = 50
                self.armStatistView.isHidden = true
                self.isArm = false
                self.armBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.armLayout.constant = 180
                self.armStatistView.isHidden = false
                self.isArm = true
                self.armBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        //大腿围
        self.thighBtn.rx.tap.subscribe(onNext: { event in
            if self.isThigh == true {
                self.thighLayout.constant = 50
                self.thighStatistView.isHidden = true
                self.isThigh = false
                self.thighBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.thighLayout.constant = 180
                self.thighStatistView.isHidden = false
                self.isThigh = true
                self.thighBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        
        //小腿围
        self.shankBtn.rx.tap.subscribe(onNext: { event in
            if self.isShank == true {
                self.shankLayout.constant = 50
                self.shankStatistView.isHidden = true
                self.isShank = false
                self.shankBtn.setImage(UIImage(named: "bod_data_xia_jian_image"), for: .normal)
            }else{
                self.shankLayout.constant = 180
                self.shankStatistView.isHidden = false
                self.isShank = true
                self.shankBtn.setImage(UIImage(named: "body_data_shang_image"), for: .normal)
            }
        }).disposed(by: dispose)
        
        getBodyDatastatisticsData()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
    }
    
    func getBodyDatastatisticsData() -> Void {
        NetWorkRequest(MCBodyDataApi.getBodyDataStatistics) { ResponseModel in
            let statisticsData = MCBodyDataStatisticsModelList(JSONString: ResponseModel.dataString)
            
            self.statisticsDataSource = statisticsData
            
            //体重
            if (statisticsData?.weight == nil || statisticsData?.weightTrend == nil) {
                self.weightStr.isHidden = true
                self.weightTrend.isHidden = true
            }else {
                self.weightNoDataLabel.isHidden = true
                self.weightStr.text = statisticsData?.weight ?? ""
                if statisticsData?.weightTrend == 0 {
                    self.weightTrend.text = "减少"
                }else if statisticsData?.weightTrend == 1 {
                    self.weightTrend.text = "无变化"
                }else if statisticsData?.weightTrend == 2 {
                    self.weightTrend.text = "增加"
                }else {
                    self.weightTrend.text = "无变化"
                }
            }
            
            //体脂
            if (statisticsData?.fat == nil || statisticsData?.fattTrend == nil) {
                self.fatLabel.isHidden = true
                self.fatreadLabel.isHidden = true
            }else {
                self.fatNoDataLabel.isHidden = true
                self.fatLabel.text = statisticsData?.fat ?? ""
                if statisticsData?.fattTrend == 0 {
                    self.fatreadLabel.text = "减少"
                }else if statisticsData?.fattTrend == 1 {
                    self.fatreadLabel.text = "无变化"
                }else if statisticsData?.fattTrend == 2 {
                    self.fatreadLabel.text = "增加"
                }else {
                    self.fatreadLabel.text = "无变化"
                }
            }
            
            
            //身高
            if (statisticsData?.height == nil || statisticsData?.heightTrend == nil) {
                self.heightLabel.isHidden = true
                self.heightreadLabel.isHidden = true
            }else {
                self.heightNoDataLabel.isHidden = true
                if statisticsData?.heightTrend == 0 {
                    self.heightreadLabel.text = "减少"
                }else if statisticsData?.heightTrend == 1 {
                    self.heightreadLabel.text = "无变化"
                }else if statisticsData?.heightTrend == 2 {
                    self.heightreadLabel.text = "增加"
                }else {
                    self.heightreadLabel.text = "无变化"
                }
            }
            
            //胸围
            if (statisticsData?.bust == nil || statisticsData?.bustTrend == nil) {
                self.bustLabel.isHidden = true
                self.bustreadLabel.isHidden = true
            }else {
                self.bustNoDataLabel.isHidden = true
                if statisticsData?.bustTrend == 0 {
                    self.bustreadLabel.text = "减少"
                }else if statisticsData?.bustTrend == 1 {
                    self.bustreadLabel.text = "无变化"
                }else if statisticsData?.bustTrend == 2 {
                    self.bustreadLabel.text = "增加"
                }else {
                    self.bustreadLabel.text = "无变化"
                }
            }
            
            
            //胸围
//            if (statisticsData?.waist == nil || statisticsData?.waist == nil) {
//                self.waistLabel.isHidden = true
//                self.waistreadLabel.isHidden = true
//            }else {
//                self.waistNoDataLabel.isHidden = true
//                if statisticsData?.waist == 0 {
//                    self.fatreadLabel.text = "减少"
//                }else if statisticsData?.bustTrend == 1 {
//                    self.fatreadLabel.text = "无变化"
//                }else if statisticsData?.bustTrend == 2 {
//                    self.fatreadLabel.text = "增加"
//                }else {
//                    self.fatreadLabel.text = "无变化"
//                }
//            }
        
            //腹围
            if (statisticsData?.abdomen == nil || statisticsData?.abdomenTrend == nil) {
                self.abdomenLabel.isHidden = true
                self.abdomenreadLabel.isHidden = true
            }else {
                self.abdomenNoDataLabel.isHidden = true
                if statisticsData?.abdomenTrend == 0 {
                    self.abdomenreadLabel.text = "减少"
                }else if statisticsData?.abdomenTrend == 1 {
                    self.abdomenreadLabel.text = "无变化"
                }else if statisticsData?.abdomenTrend == 2 {
                    self.abdomenreadLabel.text = "增加"
                }else {
                    self.abdomenreadLabel.text = "无变化"
                }
            }
            
            
            //臀围
            if (statisticsData?.pygal == nil || statisticsData?.pygalTrend == nil) {
                self.pygalLabel.isHidden = true
                self.pygalreadLabel.isHidden = true
            }else {
                self.pygalNoDataLabel.isHidden = true
                if statisticsData?.pygalTrend == 0 {
                    self.pygalreadLabel.text = "减少"
                }else if statisticsData?.pygalTrend == 1 {
                    self.pygalreadLabel.text = "无变化"
                }else if statisticsData?.pygalTrend == 2 {
                    self.pygalreadLabel.text = "增加"
                }else {
                    self.pygalreadLabel.text = "无变化"
                }
            }
            
            
            //手臂围
            
            if (statisticsData?.arm == nil || statisticsData?.armTrend == nil) {
                self.armLabel.isHidden = true
                self.armreadLabel.isHidden = true
            }else {
                self.armNoDataLabel.isHidden = true
                if statisticsData?.armTrend == 0 {
                    self.armreadLabel.text = "减少"
                }else if statisticsData?.armTrend == 1 {
                    self.armreadLabel.text = "无变化"
                }else if statisticsData?.armTrend == 2 {
                    self.armreadLabel.text = "增加"
                }else {
                    self.armreadLabel.text = "无变化"
                }
            }
            
            
            //大腿围
            
            if (statisticsData?.thigh == nil || statisticsData?.thighTrend == nil) {
                self.thighLabel.isHidden = true
                self.thighreadLabel.isHidden = true
            }else {
                self.thighNoDataLabel.isHidden = true
                if statisticsData?.thighTrend == 0 {
                    self.thighreadLabel.text = "减少"
                }else if statisticsData?.thighTrend == 1 {
                    self.thighreadLabel.text = "无变化"
                }else if statisticsData?.thighTrend == 2 {
                    self.thighreadLabel.text = "增加"
                }else {
                    self.thighreadLabel.text = "无变化"
                }
            }
            
            
            //小腿围
            
            if (statisticsData?.shank == nil || statisticsData?.shankTrend == nil) {
                self.shankLabel.isHidden = true
                self.shankreadLabel.isHidden = true
            }else {
                self.shankNoDataLabel.isHidden = true
                if statisticsData?.shankTrend == 0 {
                    self.shankreadLabel.text = "减少"
                }else if statisticsData?.shankTrend == 1 {
                    self.shankreadLabel.text = "无变化"
                }else if statisticsData?.shankTrend == 2 {
                    self.shankreadLabel.text = "增加"
                }else {
                    self.shankreadLabel.text = "无变化"
                }
            }
            
            
            
            
            
            
            
        } failureCallback: { ResponseModel in
            self.view.makeToast("网路失败",position: .center)
        }
    }
    
    @IBAction func weightSender(_ sender: UIButton) {
        
       
        
        
    }
    
    
    
}

